package com.future.mayi.qsale.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Pair;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import com.future.baselib.utils.CommonUtils;
import com.future.baselib.utils.LogUtils;
import com.future.baselib.utils.ToastUtils;
import com.future.mayi.R;
import com.future.mayi.qsale.dialog.PrizeDrawResultDialog;
import com.future.mayi.qsale.model.RedPacketInfoModel;
import com.future.mayi.qsale.utils.LoadImageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 九宫格红包抽奖
 * Created by Administrator on 2018/6/20.
 */

public class LotteryView extends View {

    private int mScreenWidth;   // 屏幕宽度
    private int mScreenHeight;  // 屏幕高度

    private int mSelfTotalWidth;    // 自身最大的宽度

    private int mSelfTotalHeight;    // 自身最大的宽度


    private static float DEFAULT_SIZE_FACTOR = 0.93f;   // 自身占用屏幕宽度的比例

    private int mOuterCircleWidth;  // 最外边圆环
    private Paint mOuterCirclePaint;
    private int mOuterCircleBackgroundColor;

    private Paint mInnerPaint;
    private int mInnerCircleBackgroundColor;

    private Paint mSmallCirclePaint;
    private int mSmallCircleBlueColor;
    private int mSmallCircleYellowColor;
    private int mInnerCardTextColor;
    private int mCenterCardBackgroundColor;
    private int mInnerCardDefaultColor;

    private int mSmallCircleRadius;  // 小圆圈半径
    private int mInnerCardWidth;    // 卡片宽度


    private int mInnerCardHeight;

    private int mInnerCardSpace;    // 卡片间隔


    private boolean mHadInitial = false;
    private ArrayList<Pair<Pair<Integer, Integer>, Pair<Integer, Integer>>> mCardPositionInfoList;
    private Context mContext;
    private AlertDialog mAlertDialog;

    //    private int[] mPicResId;
//    private String[] mInfoResArray;
    private Rect mBounds = new Rect();
    private float mSmallInfoTextSize;
    private float mBigInfoTextSize;

    private boolean mNeedRandomTimes = false;
    private int mInvalidateCircleCount;
    private int mInvalidateInnerCardCount;
    private int mLotteryInvalidateTimes;
    private boolean mStartAnimation = false; // not real animation
    private boolean mLastEndSelected = false;


    public LotteryView(Context context) {
        this(context, null);
    }

    public LotteryView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LotteryView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mContext = context;
        acquireCustomAttValues(context, attrs);

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        mScreenWidth = dm.widthPixels;
        mScreenHeight = dm.heightPixels;
        mSelfTotalWidth = mScreenWidth < mScreenHeight ?
                (int) (mScreenWidth * DEFAULT_SIZE_FACTOR) : (int) (mScreenHeight * DEFAULT_SIZE_FACTOR);


        mSmallInfoTextSize = context.getResources().getDimension(R.dimen.lotteryview_inner_card_text_size);
        mBigInfoTextSize = context.getResources().getDimension(R.dimen.lotteryview_inner_card_big_text_size);
        mOuterCircleWidth = (int) context.getResources().getDimension(R.dimen.lotteryview_outer_circle_width);
        mInnerCardSpace = (int) context.getResources().getDimension(R.dimen.lotteryview_inner_card_blank);
        mInnerCardWidth = (mSelfTotalWidth - getPaddingLeft() - getPaddingRight() - mOuterCircleWidth * 2 - mInnerCardSpace * 4) / 3;
        mInnerCardHeight = mInnerCardWidth / 3 * 2;


        mSelfTotalHeight = mInnerCardHeight * 3 + mInnerCardSpace * 4 + mOuterCircleWidth * 2 + getPaddingTop() + getPaddingBottom();


        mSmallCircleRadius = (int) context.getResources().getDimension(R.dimen.lotteryview_outer_small_circle_radius);

        mInnerCardTextColor = context.getResources().getColor(R.color.inner_card_text_color);
        mCenterCardBackgroundColor = context.getResources().getColor(R.color.center_card_bg_color);

        mOuterCircleBackgroundColor = context.getResources().getColor(R.color.outer_circle_bg_color);
        mOuterCirclePaint = new Paint();
        mOuterCirclePaint.setColor(mOuterCircleBackgroundColor);
        mOuterCirclePaint.setAntiAlias(true);
        mOuterCirclePaint.setStrokeWidth(mOuterCircleWidth);
        mOuterCirclePaint.setStyle(Paint.Style.FILL);

        mSmallCircleBlueColor = mSmallCircleBlueColor != 0 ? mSmallCircleBlueColor : context.getResources().getColor(R.color.small_circle_color_blue);
        mSmallCircleYellowColor = mSmallCircleYellowColor != 0 ? mSmallCircleYellowColor : context.getResources().getColor(R.color.small_circle_color_yellow);
        mSmallCirclePaint = new Paint();
        mSmallCirclePaint.setColor(mSmallCircleBlueColor);
        mSmallCirclePaint.setAntiAlias(true);
        mOuterCirclePaint.setStyle(Paint.Style.FILL);


        mInnerCircleBackgroundColor = context.getResources().getColor(R.color.inner_circle_bg_color);
        mInnerPaint = new Paint();
        mInnerPaint.setAntiAlias(true);
        mInnerPaint.setColor(mInnerCircleBackgroundColor);
        mInnerPaint.setStyle(Paint.Style.FILL);

        mCardPositionInfoList = new ArrayList<>();
//        initResId();
    }

    private void acquireCustomAttValues(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LotteryView);
        mSmallCircleBlueColor = ta.getColor(R.styleable.LotteryView_outer_small_circle_color_default, 0);
        mSmallCircleYellowColor = ta.getColor(R.styleable.LotteryView_outer_small_circle_color_active, 0);
        mLotteryInvalidateTimes = ta.getInt(R.styleable.LotteryView_lottery_invalidate_times, 0);
        DEFAULT_SIZE_FACTOR = ta.getFloat(R.styleable.LotteryView_self_width_size_factor, DEFAULT_SIZE_FACTOR);
        mInnerCardDefaultColor = ta.getColor(R.styleable.LotteryView_inner_round_card_color_default, Color.parseColor("#ffffff"));
        ta.recycle();
    }

//    private void initResId() {
//        mPicResId = new int[]{
//                R.mipmap.icon_huawei_mobile, R.mipmap.icon_xiaomi_bracelet, R.mipmap.icon_qq_sport,
//                R.mipmap.icon_gopro_camera, 0, R.mipmap.icon_misfit_flash,
//                R.mipmap.icon_qq_sport, R.mipmap.icon_qq_gongzai, 0
//        };
//
//        mInfoResArray = mContext.getResources().getStringArray(R.array.jifeng_array_info);
//    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mSelfTotalWidth, mSelfTotalWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            drawOuterRoundCircle(canvas);
        }
        drawOuterDecorateSmallCircle(canvas);
        drawInnerBackground(canvas);
        drawInnerCards(canvas);
        loopSmallCircleAnimation();
        loopInnerRoundCardAnimation();
    }

    /**
     * 外层带圆角矩形圆环
     */
    private void drawOuterRoundCircle(Canvas canvas) {
        canvas.save();
        canvas.clipRect(
                mOuterCircleWidth + getPaddingLeft(),
                mOuterCircleWidth + getPaddingTop(),
                mSelfTotalWidth - mOuterCircleWidth - getPaddingRight(),
                mSelfTotalHeight - mOuterCircleWidth - getPaddingBottom(),
                Region.Op.DIFFERENCE);

        canvas.drawRoundRect(
                new RectF(getPaddingLeft(),
                        getPaddingTop(),
                        mSelfTotalWidth - getPaddingRight(),
                        mSelfTotalHeight - getPaddingBottom()),
                18, 18, mOuterCirclePaint);
        canvas.restore();
    }


    private void drawOuterDecorateSmallCircle(Canvas canvas) {
        int result = mInvalidateCircleCount % 2;

        // top
        int x = 0, y = 0;
        int sideSize = mSelfTotalWidth - mOuterCircleWidth * 2 - getPaddingLeft() - getPaddingRight(); // 除去最外边圆环后的边长
        int heightSize = mSelfTotalHeight - mOuterCircleWidth * 2 - getPaddingTop() - getPaddingBottom();
        for (int i = 0; i < 10; i++) {
            mSmallCirclePaint.setColor(i % 2 == result ? mSmallCircleYellowColor : mSmallCircleBlueColor);
            x = mOuterCircleWidth + (sideSize - mSmallCircleRadius * 2 * 9) / 9 * i + mSmallCircleRadius * 2 * i + getPaddingLeft();
            y = (mOuterCircleWidth - mSmallCircleRadius * 2) / 2 + mSmallCircleRadius + getPaddingTop();
            canvas.drawCircle(x, y, mSmallCircleRadius, mSmallCirclePaint);
        }

        // bottom
        for (int i = 0; i < 10; i++) {
            mSmallCirclePaint.setColor(i % 2 == result ? mSmallCircleYellowColor : mSmallCircleBlueColor);
            x = mOuterCircleWidth + (sideSize - mSmallCircleRadius * 2 * 9) / 9 * i + mSmallCircleRadius * 2 * i + getPaddingLeft();
            y = mSelfTotalHeight - mOuterCircleWidth + (mOuterCircleWidth - mSmallCircleRadius * 2) / 2 + mSmallCircleRadius - getPaddingBottom();
            canvas.drawCircle(x, y, mSmallCircleRadius, mSmallCirclePaint);
        }

        // left
        for (int i = 0; i < 9; i++) {
            mSmallCirclePaint.setColor(i % 2 == (result == 0 ? 1 : 0) ? mSmallCircleYellowColor : mSmallCircleBlueColor);
            x = mOuterCircleWidth / 2 + getPaddingLeft();
            y = mOuterCircleWidth * 2 + (heightSize - mSmallCircleRadius * 2 * 9) / 9 * i + mSmallCircleRadius * 2 * i + getPaddingTop();
            canvas.drawCircle(x, y, mSmallCircleRadius, mSmallCirclePaint);
        }

        // right
        for (int i = 0; i < 9; i++) {
            mSmallCirclePaint.setColor(i % 2 == result ? mSmallCircleYellowColor : mSmallCircleBlueColor);
            x = mSelfTotalWidth - mOuterCircleWidth / 2 - getPaddingRight();
            y = mOuterCircleWidth * 2 + (heightSize - mSmallCircleRadius * 2 * 9) / 9 * i + mSmallCircleRadius * 2 * i + getPaddingTop();
            canvas.drawCircle(x, y, mSmallCircleRadius, mSmallCirclePaint);
        }
    }

    private void drawInnerBackground(Canvas canvas) {
        mInnerPaint.setColor(Color.parseColor("#FF9A9A"));
        canvas.drawRect(mOuterCircleWidth + getPaddingLeft(), mOuterCircleWidth + getPaddingTop(),
                mSelfTotalWidth - mOuterCircleWidth - getPaddingRight(),
                mSelfTotalHeight - mOuterCircleWidth - getPaddingBottom(), mInnerPaint);
    }

    private void drawInnerCards(Canvas canvas) {
        int left = 0, top = 0, right = 0, bottom = 0;
        int spaceNum = 0;
        for (int i = 0; i < 9; i++) {
            spaceNum = i % 3 + 1;
            left = mOuterCircleWidth + mInnerCardWidth * (i % 3) + mInnerCardSpace * spaceNum + getPaddingLeft();
            top = mOuterCircleWidth + mInnerCardHeight * (i / 3) + mInnerCardSpace * (i / 3 + 1) + getPaddingTop();
            right = left + mInnerCardWidth;
            bottom = top + mInnerCardHeight;
            if (!mHadInitial) {
                mCardPositionInfoList.add(new Pair(new Pair(left, right), new Pair(top, bottom)));
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                drawInnerRoundCard(canvas, left, top, right, bottom, i);
            }
        }
        mHadInitial = true;
    }

    public static final int PIC_WIDTH = 130;

    private void drawInnerRoundCard(Canvas canvas, int left, int top, int right, int bottom, int index) {


        boolean need = switchCardColorIfNeed(index);

        mInnerPaint.setColor(mInnerCardDefaultColor);

        if (mStartAnimation && need) {
            mInnerPaint.setColor(Color.parseColor("#FF8921"));
        }

        // 绘制内部小卡片
        if (index == 4) {
            mInnerPaint.setColor(mCenterCardBackgroundColor);
        }

        canvas.drawRoundRect(new RectF(left, top, right, bottom), 12, 12, mInnerPaint);

        if (index == 4) {
            mInnerPaint.setColor(mCenterCardBackgroundColor);
            int space = mInnerCardWidth / 9;
            canvas.drawRoundRect(new RectF(left + space, top + space, right - space, bottom - space), 12, 12, mInnerPaint);
        }
        // 绘制卡片中的图片 固定宽高PIC_WIDTH
//        int picHeight = PIC_WIDTH;
        if (files != null && !TextUtils.isEmpty(files[index])) {
            Bitmap bitmap = BitmapFactory.decodeFile(files[index]);
//            int picLeft = left + (mInnerCardWidth - PIC_WIDTH) / 2;
//            int picTop = top + (mInnerCardHeight - picHeight) / 2;
            Rect mSrcRect, mDestRect;

            int picWidth, picHeight;

            int imagePadding = CommonUtils.dip2px(getContext(), IMAGE_PADDING);

            float carProportion = (float) mInnerCardWidth / (float) mInnerCardHeight;
            float proportion = (float) bitmap.getWidth() / (float) bitmap.getHeight();
            LogUtils.d(TAG, "proportion:" + proportion);
            if (proportion >= carProportion) {
                picWidth = mInnerCardWidth - imagePadding * 2;
                picHeight = (int) (picWidth / proportion);
            } else {
                picHeight = mInnerCardHeight - imagePadding * 2;
                picWidth = (int) (picHeight * proportion);
            }

            int picLeft = left + (mInnerCardWidth - picWidth) / 2;
            int picTop = top + (mInnerCardHeight - picHeight) / 2;

            mSrcRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            mDestRect = new Rect(picLeft, picTop, picLeft + picWidth, picTop + picHeight);
            canvas.drawBitmap(BitmapFactory.decodeFile(files[index]), mSrcRect, mDestRect, mInnerPaint);
        }

        // 绘制卡片说明文字
        if (mPrizeInfoBeanList != null && !TextUtils.isEmpty(mPrizeInfoBeanList.get(index).getPrize_name())) {
            if (index == 4) { // center text
                mInnerPaint.setColor(index == 4 ? Color.parseColor("#ffffff") : mInnerCardTextColor);
                mInnerPaint.setTextSize(mBigInfoTextSize);
                int textX = left + (mInnerCardWidth - getTextWidth(mPrizeInfoBeanList.get(index).getPrize_name().substring(0, 2), mInnerPaint)) / 2;
                int textY_1 = top + (mInnerCardHeight / 4 + getTextHeight(mPrizeInfoBeanList.get(index).getPrize_name().substring(0, 2), mInnerPaint)) - 8;
                int textY_2 = top + (mInnerCardHeight / 4 + getTextHeight(mPrizeInfoBeanList.get(index).getPrize_name().substring(0, 2), mInnerPaint) * 2 + mInnerCardHeight / 10) - 8;
                canvas.drawText(mPrizeInfoBeanList.get(index).getPrize_name(), 0, 2, textX, textY_1, mInnerPaint);
                canvas.drawText(mPrizeInfoBeanList.get(index).getPrize_name(), 2, 4, textX, textY_2, mInnerPaint);
                return;
            }
            mInnerPaint.setColor(mInnerCardTextColor);
            mInnerPaint.setTextSize(mSmallInfoTextSize);
//            int textX = left + (mInnerCardWidth - getTextWidth(mPrizeInfoBeanList.get(index).getPrize_name(), mInnerPaint)) / 2;
//            int textY = top + picHeight + mInnerCardWidth / 4 + getTextHeight(mPrizeInfoBeanList.get(index).getPrize_name(), mInnerPaint) - 10;
//            canvas.drawText(mPrizeInfoBeanList.get(index).getPrize_name(), textX, textY, mInnerPaint);
        }
    }

    public static final int IMAGE_PADDING = 5;

    private boolean switchCardColorIfNeed(int index) {
        int result = mInvalidateInnerCardCount % 9;
        if ((result == 0 && index == 0) || (result == 1 && index == 1) || (result == 2 && index == 2)
                || (result == 6 && index == 6)) {
            return true;
        }
        if ((result == 3 && index == 5) || (result == 4 && index == 8) || (result == 5 && index == 7)
                || (result == 7 && index == 3)) {
            return true;
        }
        return false;
    }

    private void loopSmallCircleAnimation() {
        // not real animation, just like it.
        if (mStartAnimation) {
            if (mInvalidateInnerCardCount % 8 == 0) {
                mInvalidateCircleCount++;
                postInvalidate();
            }
        } else {
            mInvalidateCircleCount++;
            postInvalidateDelayed(800);
        }
    }

    private void loopInnerRoundCardAnimation() {
        if (!mStartAnimation || mLastEndSelected) return;

        if (mInvalidateInnerCardCount == mLotteryInvalidateTimes) {
            mLastEndSelected = true;
            postInvalidate();
            postDelayed(new ResultTask(mLotteryInvalidateTimes), 300);
            return;
        }

        mInvalidateInnerCardCount++;
        postInvalidateDelayed(50);

    }

    private class ResultTask implements Runnable {
        int times;

        public ResultTask(int times) {
            this.times = times;
        }

        @Override
        public void run() {

            String info = "";
            int i = times % 9;
            info = mPrizeInfoBeanList.get(getRealIndex(i)).getPrize_name();
            showResultDialog(mContext, info);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                break;

            case MotionEvent.ACTION_UP:
                int index = getTouchPositionInCardList(x, y);
                if (index == 5) {
//                    if (mNeedRandomTimes || mLotteryInvalidateTimes == 0) {
//                        mLotteryInvalidateTimes = (new Random().nextInt(9) + 1) * 9 + new Random().nextInt(9);
//                        mNeedRandomTimes = true;
//                    }
                    showReminderDialog(mContext);
                }
                break;
        }

        return true;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mStartAnimation = false;
        mInvalidateCircleCount = 0;
        mInvalidateInnerCardCount = 0;
        mNeedRandomTimes = false;

    }

    private void showResultDialog(Context context, String result) {
//        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
//        builder.setTitle(R.string.result_title)
//                .setMessage(context.getString(R.string.result_message, result))
//                .setCancelable(true)
//                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        mStartAnimation = false;
//                        invalidate();
//                        dialog.dismiss();
//                    }
//                });
//        builder.create().show();
        PrizeDrawResultDialog prizeDrawResultDialog = new PrizeDrawResultDialog(context, mResultPrizeInfo);
        prizeDrawResultDialog.show();
        prizeDrawResultDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mStartAnimation = false;
                invalidate();
                mInvalidateInnerCardCount = 0;
                mLastEndSelected = false;
            }
        });
    }

    private void showReminderDialog(Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(R.string.choujiang)
                .setMessage(R.string.choujiang_desc)
                .setCancelable(false)
                .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (mAlertDialog != null) mAlertDialog.dismiss();
                    }
                })
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (mPrizeListener != null) {
                            mPrizeListener.getPrizeDraw();
                        }
                    }
                });
        mAlertDialog = builder.create();
        mAlertDialog.show();
    }

    //中奖结果
    private RedPacketInfoModel.PrizeInfoBean mResultPrizeInfo;

    /**
     * 开始抽奖动画
     *
     * @param prizeInfoBean
     */
    public void startPrizeAnimation(RedPacketInfoModel.PrizeInfoBean prizeInfoBean) {
        int resultIndex = getResultIndex(prizeInfoBean.getPrize_id());
        if (resultIndex == -1 || resultIndex > 8) {
            ToastUtils.shortToast(getContext(), "网络错误!");
            return;
        }
        mResultPrizeInfo = prizeInfoBean;
        mLotteryInvalidateTimes = 3 * 9 + getResultCount(resultIndex);
        mStartAnimation = true;
        invalidate();
    }

    private int getTouchPositionInCardList(int x, int y) {
        if (mCardPositionInfoList != null) {
            int index = 1;
            for (Pair<Pair<Integer, Integer>, Pair<Integer, Integer>> pair : mCardPositionInfoList) {
                if (x > pair.first.first && x < pair.first.second && y > pair.second.first && y < pair.second.second) {
                    return index;
                }
                index++;
            }
        }
        return 0;
    }


    private int getTextWidth(String str, Paint paint) {
        paint.getTextBounds(str, 0, str.length(), mBounds);
        return mBounds.width();
    }

    private int getTextHeight(String text, Paint paint) {
        paint.getTextBounds(text, 0, text.length(), mBounds);
        return mBounds.height();
    }

    private static final String TAG = "LotteryView";
    private List<RedPacketInfoModel.PrizeInfoBean> mPrizeInfoBeanList;
    private String[] files;

    public void setPrizeInfo(List<RedPacketInfoModel.PrizeInfoBean> prizeInfoBeanList) {
        this.mPrizeInfoBeanList = prizeInfoBeanList;
        RedPacketInfoModel.PrizeInfoBean prizeInfoBean = new RedPacketInfoModel.PrizeInfoBean();
        prizeInfoBean.setPrize_name("点击抽奖");
        mPrizeInfoBeanList.add(4, prizeInfoBean);
        LoadImageUtils loadImageUtils = new LoadImageUtils(getContext(), getPrizeImgs(prizeInfoBeanList));
        loadImageUtils.setLoadImageListener(new LoadImageUtils.LoadImageListener() {
            @Override
            public void loadSucceed(List<LoadImageUtils.PrizeImgModel> prizeImgModels) {
                files = new String[9];

                for (int i = 0; i < prizeImgModels.size(); i++) {
                    LogUtils.d(TAG, "i:" + i + "prizeImgModels.get(i).getUrls():" + prizeImgModels.get(i).getUrls());
                    int index = getIndexOf(prizeImgModels.get(i).getPrizeId());
                    LogUtils.d(TAG, "index" + index);
                    if (index != -1 && index < 9) {
                        LogUtils.d(TAG, "index:" + index + "localPath:" + prizeImgModels.get(i).getLocalPath());
                        files[index] = prizeImgModels.get(i).getLocalPath();
                    }
                }
                invalidate();
            }
        }).startDownload();
    }

    private int getIndexOf(int prizeId) {
        int index = -1;
        for (int i = 0; i < mPrizeInfoBeanList.size(); i++) {
            if (prizeId == mPrizeInfoBeanList.get(i).getPrize_id()) {
                index = i;
                return index;
            }
        }
        return index;
    }

    private List<LoadImageUtils.PrizeImgModel> getPrizeImgs(List<RedPacketInfoModel.PrizeInfoBean> prizeInfoBeanList) {
        List<LoadImageUtils.PrizeImgModel> imageUrls = new ArrayList<>();
        for (int i = 0; i < prizeInfoBeanList.size(); i++) {
            LoadImageUtils.PrizeImgModel prizeImgModel = new LoadImageUtils.PrizeImgModel();
            prizeImgModel.setPrizeId(prizeInfoBeanList.get(i).getPrize_id());
            prizeImgModel.setUrls(prizeInfoBeanList.get(i).getPrize_img());
            imageUrls.add(prizeImgModel);
        }
        return imageUrls;
    }

    private int getResultIndex(int prizeId) {
        int index = -1;
        for (int i = 0; i < mPrizeInfoBeanList.size(); i++) {
            if (prizeId == mPrizeInfoBeanList.get(i).getPrize_id()) {
                index = i;
                return index;
            }
        }
        return index;
    }

    //根据index获取mLotteryInvalidateTimes的余数
    private int getResultCount(int index) {
        int result = index;
        if (index == 5) {
            result = 3;
        }
        if (index == 8) {
            result = 4;
        }
        if (index == 7) {
            result = 5;
        }
        if (index == 3) {
            result = 7;
        }
        return result;
    }

    private int getRealIndex(int resultCount) {
        int index;
        index = resultCount;
        if (resultCount == 3) {
            index = 5;
        }
        if (resultCount == 4) {
            index = 8;
        }
        if (resultCount == 5) {
            index = 7;
        }
        if (resultCount == 7) {
            index = 3;
        }
        return index;
    }

    private PrizeListener mPrizeListener;

    public void setPrizeListener(PrizeListener prizeListener) {
        this.mPrizeListener = prizeListener;
    }

    public interface PrizeListener {
        void getPrizeDraw();
    }

}
